package com.bit.frige;

import java.io.InputStream;
import java.util.ArrayList;
import android.app.ProgressDialog;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.bit.frige.DB.DBContactHelper;
import com.bit.frige.DB.FoodDTO;
import com.bit.frige.recipe.RecipeListDTO;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;

public class LoadingActivity extends Activity {
	ArrayList<FoodDTO> list;
	ArrayList<RecipeListDTO> list2;
	InputStream is;
	DBContactHelper db;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loading);


		Thread thread = new Thread(new Runnable(){
			public void run(){
				list = getExcel();
				list2=getExcelRecipe();
				
				
				db = new DBContactHelper(LoadingActivity.this);
				Log.d("recipe","123");
				if(0==db.getInfo(1)){
					
					db.addInfo();
					for (FoodDTO food : list) {
						db.addContact(food); 
					} 
				}
				for (RecipeListDTO recipe : list2) {
					Log.d("recipe",""+list2);
					db.addRecipe(recipe); 
				} 

				handler.sendEmptyMessageDelayed(0, 1000);

				//                                handler.sendEmptyMessage(0);
			} 
		});
		thread.start();

	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			//                loadingDialog.dismiss();//다이얼로그 삭제 
			Intent intent = new Intent(getApplicationContext(),MainActivity.class);
			intent.putExtra("load",1);
			startActivity(intent);
			finish();
		}
	};

	//음식 종류 List 엑셀 파일에서 읽어오기
	public ArrayList<FoodDTO> getExcel(){
		HSSFWorkbook wb;
		ArrayList<FoodDTO> list = new ArrayList<FoodDTO>();
		try {
			is = getBaseContext().getResources().getAssets().open("frige.xls");
			wb = new HSSFWorkbook(is);
			is.close();
			HSSFSheet sheet =wb.getSheetAt(0);
			if(wb!=null&&sheet!=null){
				int srow = 0;
				int erow = sheet.getLastRowNum();
				HSSFRow row=sheet.getRow(0);
				int scol = 0;
				int ecol = row.getLastCellNum();
				FoodDTO food;
				for(int i=srow; i<=erow;i++){
					String [] data =new String[3];
					for(int j =scol; j<ecol; j++){
						HSSFRow row1=sheet.getRow(i);
						HSSFCell cell= row1.getCell(j);
						int celltype = cell.getCellType();
						switch(celltype){
						case 0 :
							int ss = (int)cell.getNumericCellValue();
							data[j] =""+ss;
							break;
						case 1 :
							data[j] = cell.getStringCellValue();
							break;
						}

					}
					food= new FoodDTO(Integer.parseInt(data[0]),data[1],Integer.parseInt(data[2]));
					list.add(food);

				}

			}else{
				System.out.println("처리불가");
			}


		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}


	//레시피 목록 List 엑셀 읽어오기
		public ArrayList<RecipeListDTO> getExcelRecipe(){
		HSSFWorkbook wb;
		ArrayList<RecipeListDTO> list2 = new ArrayList<RecipeListDTO>();
		try {
			is = getBaseContext().getResources().getAssets().open("recipe.xls");
			wb = new HSSFWorkbook(is);
			is.close();
			HSSFSheet sheet2 =wb.getSheetAt(0);
			if(wb!=null&&sheet2!=null){
				int srow = 0;
				int erow = sheet2.getLastRowNum();
				HSSFRow row=sheet2.getRow(0);
				int scol = 0;
				int ecol = row.getLastCellNum();
				RecipeListDTO RecipeList;
				for(int i=srow; i<=erow;i++){
					String [] data =new String[3];
					for(int j =scol; j<ecol; j++){
						HSSFRow row1=sheet2.getRow(i);
						HSSFCell cell= row1.getCell(j);
						int celltype = cell.getCellType();
						switch(celltype){
						case 0 :
							int ss = (int)cell.getNumericCellValue();
							data[j] =""+ss;
							break;
						case 1 :
							data[j] = cell.getStringCellValue();
							break;
						}

					}
					RecipeList= new RecipeListDTO(Integer.parseInt(data[0]),data[1],data[2]);
					list2.add(RecipeList);

				}

			}else{
				System.out.println("처리불가");
			}        

		} catch (Exception e) {
			e.printStackTrace();
		}
		Log.d("dd", "recipe2excel");
			return list2;
	}







}